<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
use Yansongda\Pay\Pay;
use Yansongda\Pay\Log;

/*
  |--------------------------------------------------------------------------
  | 写入日志
  |--------------------------------------------------------------------------
  | use Illuminate\Support\Facades\Log;
  | $arr = ['status' => 1, 'msg' => '订单下单成功~']; //一定是数组
  | Log::channel('order')->info('order', $arr); //channel('order')自定义驱动
  |
  */

class PayController extends Controller
{
   /**
    * @return mixed
    *  支付宝支付
    */
   public function alipay(Request $request)
   {
      $order = [
         'out_trade_no' => time(),  //订单号
         'total_amount' => '0.1',  //金额
         'subject' => 'test测试',   //订单标题
         'body' => '123123',  //订单描述
         'buyer_id' => '2088602187894250',  //小程序支付的user_id
      ];
      $config = config('pay.alipy');
      $alipay = Pay::alipay($config)->mini($order);

      return $alipay;  //其他 return $alipay->send();
   }

   /***
    * 支付宝通知
    */
   public function return()
   {
      $config = config('pay.alipy');
      $data = Pay::alipay($config)->verify(); //验签

      return $data;
      // 订单号：$data->out_trade_no
      // 交易号：$data->trade_no
      // 订单总金额：$data->total_amount
   }

   /***
    * @return mixed
    * 支付宝异步通知回调
    * 验证 trade_status === TRADE_SUCCESS，seller_email，app_id，out_trade_no，total_amount
    */
   public function alipay_notify(Request $request)
   {
      $config = config('pay.alipy');
      $alipay = Pay::alipay($config);
      try {
         $data = $alipay->verify(); //验签
         if ($data) {
            if ($data->trade_status === 'TRADE_SUCCESS' && $data->app_id === '' && $data->seller_email === '') {
               $order = DB::table('orders')
                  ->where('order_code', $data->out_trade_no)
                  ->where('total_price', $data->total_amount)
                  ->first();
               if ($order) {
                  $order->update([
                     'status' => 1, //支付状态
                     'trade_code' => $data->trade_no, //支付宝交易号
                     'pay_time' => Carbon::now()  //支付时间
                  ]);
               }
            };
         }

         Log::debug('Alipay notify', $data->all());
      } catch (\Exception $e) {
         // $e->getMessage();
      }

      return $alipay->success();  //其他 return $alipay->success()->send();
   }

   /***
    *  微信小程序支付
    */
   public function wechat(Request $request)
   {
      $order = [
         'out_trade_no' => time(),
         'total_fee' => '1', //单位：分
         'body' => 'test 测试',
         'openid' => '',
      ];
      $config = config('pay.wechat');
      $pay = Pay::wechat($config)->miniapp($order);

      return $pay;
      // $pay->appId，$pay->timeStamp，$pay->nonceStr，$pay->package，$pay->signType
   }

   /***
    *  微信JSSDK支付
    */
   public function wechat_mp(Request $request)
   {
      $openid = TokenController::get_openid();
      $order = [
         'out_trade_no' => time(),
         'total_fee' => '1', //单位：分
         'body' => 'test 测试',
         'openid' => $openid,
      ];
      $config = config('pay.wechat');
      $pay = Pay::wechat($config)->mp($order);

      return $pay;
   }

   /***
    *  微信支付异步通知回调
    *  验证 return_code === SUCCESS，appid，mch_id，out_trade_no，total_fee
    */
   public function wechat_notify(Request $request)
   {
      $config = config('pay.wechat');
      $pay = Pay::wechat($config);
      try {
         $data = $pay->verify(); //验签
         if ($data) {
            if (($data->return_code === 'SUCCESS' && $data->return_code === 'SUCCESS') && $data->appid === '' && $data->mch_id === '') {
               $order = DB::table('orders')
                  ->where('order_code', $data->out_trade_no)
                  ->where('total_price', $data->total_fee)
                  ->first();
               if ($order) {
                  $order->update([
                     'status' => 1,  //支付状态
                     'trade_no' => $data->transaction_id,  //交易号
                     'pay_time' => Carbon::now(),  //支付时间
                  ]);
               }
            };
         }

         Log::debug('Wechat notify', $data->all());
      } catch (\Exception $e) {
         // $e->getMessage();
      }

      return $pay->success();  //其他 return $pay->success()->send();
   }

}
